//app.js
App({
  data:{
    appid: 'wx2b50090e078fdcce',
    // 测试地址
    globalAPI: 'https://test2.boxinschool.com',
    globalUGC: 'https://testarticle.xjwblog.com',
    globalMall: 'https://testmall.xjwblog.com',
    myFansNum: 'https://testugc.xjwblog.com',

    // 正式地址
    // globalAPI: 'https://xx.boxinschool.com', //地址
    // globalUGC: 'https://ugcvue.boxinschool.com',
    // globalMall:'https://mallvue.boxinschool.com',
    // myFansNum: 'https://ugcadmin.boxinschool.com'
   
  },

  //时间戳转 时间字符串
  timeTransformat: function (time) {
    var date = new Date();
    date.setTime(time * 1000);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    // return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
  },

  //获取url中对应字段
  getURLParameter: function (name) {
    var res = decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
    if (res != null) {
      return res;
    } else {
      return "";
    }
  },

  //获取用户的openid
  getWxUserinfo: function () {
    const that = this;
    wx.login({
      success: function (res) {
        //发送请求
        wx.request({
          url: that.data.globalAPI + '/users/weixin/login', //接口地址
          data: {
            code: res.code,
            source: 'small program',//注册来源标识
            invite_id: ''//邀请人标识
          },
          header: {
            'content-type': 'application/json' //默认值
          },
          success: function (res) {
            if (res.data.error == 0) {
              wx.setStorageSync('token', res.data.data.token);
              wx.setStorageSync('userInfo', res.data.data.userinfo);
              wx.setStorageSync('allUserMsg', res.data.data);
            }
          }
        })
      }
    })
  },

  // token登录
  getTokenLogin:function(){
    const that = this;
    wx.request({
      url: that.data.globalAPI + '/users/login/login', //接口地址
      data: {
        token: wx.getStorageSync('token'),
        source: 'small program',//注册来源标识
        phone_model: wx.getSystemInfoSync().model + ',' + wx.getSystemInfoSync().brand//设备型号
      },
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        if (res.data.error == 0) {
          wx.setStorageSync('token', res.data.data.token);
          wx.setStorageSync('userInfo', res.data.data.userinfo);
          wx.setStorageSync('allUserMsg', res.data.data);
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 800
          })
        }
      }, fail() {
        wx.showModal({
          title: '提示',
          content: '网络异常，请稍后重试！',
          showCancel: false,
          success(res) {
          }
        })
      }
    })
  },

  onLaunch:function(){
    wx.removeStorageSync('currentSchool');
    wx.removeStorageSync('cityId');
  },
  
})